<?php
namespace app\api\controller;

use think\Controller;
use think\Config;
use think\Db;
use think\Session;
use think\Cookie;

/**
 * 选择接口
 * Class Interfaces
 * @package app\api\controller
 */
class Interfaces extends Controller
{
    protected $appId;
    protected $appSecret;

    protected function _initialize()
    {
        parent::_initialize();

        $this->appId = 'xxb90d8b683784a970';//appId
        $this->appSecret = 'dde84e73954aa0b136a51bbefdc79aac';//appSecret

        //判断TOKEN时间
        if(!Cookie('token_time'.Session::get('site_id'))){
            if(strtotime(Cookie('token_time'.Session::get('site_id')))<time()){
                Cookie::delete('token'.Session::get('site_id'));
            }
        }

        if(!Cookie('token'.Session::get('site_id'))){
            $token=$this->token();
            if($token['status']=='ok'){
                Cookie::set('token_time'.Session::get('site_id'),$token['data']['expireTime']);
                Cookie::set('token'.Session::get('site_id'),$token['data']['token']);
            }
        }

        //echo Cookie('token');
    }

    /**
     * 手机验证码
     */
    public function tel($tel)
    {
        if (isset($tel)==true) {
            $code=rand(1000, 9999);
            $content['appId']           =$this->appId;
            $content['businessType']    ='验证码';
            $content['mobile']          =$tel;
            $data_tel['code']           =$code;
            $content['param']           =$data_tel;
            $curl_init=$this->curl_init('xinxun.service.sms.sendsms','/open/gateway',$content);
            if($curl_init['status']=='ok'){
                Session::set('tel_code_time',date('Y-m-d H:i:s'));
                Session::set('tel_code',$code);
                $result = [
                    'status' => "ok",
                    'message' =>"发送成功"
                ];
            } else {
                $result = [
                    'status'  => "fail",
                    'message' => $curl_init['data']
                ];
            }
        }else {
            $result = [
                'status'  => "fail",
                'message' => "手机号码不能为空"
            ];
        }

        return json($result);
    }

    /**
     * 通知审核
     */
    public function tel_remind($tel,$title)
    {
        if (isset($tel)==true) {
            $content['appId']           =$this->appId;
            $content['businessType']    ='通知审核';
            $content['mobile']          =$tel;
            $data_tel['title']          =$title;
            $content['param']           =$data_tel;
            $curl_init=$this->curl_init('xinxun.service.sms.sendsms','/open/gateway',$content);
            if($curl_init['status']=='ok'){
                $result = [
                    'status' => "ok",
                    'message' =>"发送成功"
                ];
            } else {
                $result = [
                    'status'  => "fail",
                    'message' => $curl_init['data']
                ];
            }
        }else {
            $result = [
                'status'  => "fail",
                'message' => "手机号码不能为空"
            ];
        }

        return json($result);
    }



    /**
     * 图片提交
     */
    public function img($imageType=0,$imgSrc)
    {
        $data['app_id']=$this->appId;
        $data['api_request_id']=date("YmdHis");
        $data['imageType']=$imageType;
        $data['imgSrc']=$imgSrc;

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_PORT => "80",
            CURLOPT_URL => "http://openapi.yc-police.com/open/image",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => http_build_query($data),
            CURLOPT_HTTPHEADER => array(
                "authorization: Bearer ".Cookie('token'.Session::get('site_id')),
                "content-type: application/x-www-form-urlencoded"
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            $result = [
                'status'   => "fail",
                'message' => "获取错误",
                'data'   => "cURL Error #:" . $err
            ];
        } else {
            $response=json_decode($response,true);
            if($response['code']==100){
                $result = [
                    'status' => "ok",
                    'message' =>"获取成功",
                    'data'   => $response['data'][0]
                ];
            }else{
                $result = [
                    'status'   => "fail",
                    'message' => "获取失败",
                    'data'   => $response['msg']
                ];
            }
        }

        return $result;
    }


    /**
     * 获取token
     */
    public function token()
    {
        $data['app_id']=$this->appId;
        $data['method']='xinxun.common.token.get';
        $data['version']='1.0';
        $data['api_request_id']=date("YmdHis");

        $content['appId']=$this->appId;
        $content['appSecret']=$this->appSecret;
        $data['content']=json_encode($content);

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_PORT => "80",
            CURLOPT_URL => "http://openapi.yc-police.com/open/gateway",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => http_build_query($data),
            CURLOPT_HTTPHEADER => array(
                "content-type: application/x-www-form-urlencoded"
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            $result = [
                'status'   => "fail",
                'message' => "获取错误",
                'data'   => "cURL Error #:" . $err
            ];
        } else {
            $response=json_decode($response,true);
            if($response['code']==100){
                $result = [
                    'status' => "ok",
                    'message' =>"获取成功",
                    'data'   => $response['data']
                ];
            }else{
                $result = [
                    'status'   => "fail",
                    'message' => "获取失败",
                    'data'   => $response['msg']
                ];
            }
        }

        return $result;
    }


    /**
     * 传送curl
     */
    public function curl_init($method,$url,$content)
    {
        $data['app_id']=$this->appId;
        $data['method']=$method;
        $data['version']='1.0';
        $data['api_request_id']=date("YmdHis");
        $data['content']=json_encode($content);


        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_PORT => "80",
            CURLOPT_URL => "http://openapi.yc-police.com".$url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => http_build_query($data),
            CURLOPT_HTTPHEADER => array(
                "authorization: Bearer ".Cookie('token'.Session::get('site_id')),
                "content-type: application/x-www-form-urlencoded"
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);


        if ($err) {
            $result = [
                'status'   => "fail",
                'message' => "获取错误",
                'data'   => "cURL Error #:" . $err
            ];
        } else {
            $response=json_decode($response,true);
            if($response['code']==100){
                $result = [
                    'status' => "ok",
                    'message' =>"获取成功",
                    'data'   => $response['data']
                ];
            }else{
                //301/302/303清除token缓存
                if($response['code']==301 || $response['code']==302 ||$response['code']==303){
                    Cookie::delete('token'.Session::get('site_id'));
                }
                $result = [
                    'status'   => "fail",
                    'message' => "获取失败",
                    'data'   => $response['msg']
                ];
            }
        }

        return $result;

    }



}